package com.sjht.android.bxgz.activity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import android.os.AsyncTask;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.lwx.ui.library.pull_to_refresh.PullToRefreshBase;
import com.lwx.ui.library.pull_to_refresh.PullToRefreshBase.OnLastItemVisibleListener;
import com.lwx.ui.library.pull_to_refresh.PullToRefreshBase.OnRefreshListener;
import com.lwx.ui.library.pull_to_refresh.PullToRefreshListView;
import com.sjht.android.bxgz.BaseActivity;
import com.sjht.android.bxgz.R;
import com.sjht.android.bxgz.activity.adapter.NewsAdapter;
import com.sjht.android.bxgz.activity.adapter.PictureAdapter;
import com.sjht.android.bxgz.view.PictureGallery;

public class HomeActivity extends BaseActivity {
	private String title[] = { "one", "two", "three", "four", "five", "six" };
	private LinearLayout linearLayout;
	private ArrayList<TextView> tv_indicators;

	static final int MENU_MANUAL_REFRESH = 0;
	static final int MENU_DISABLE_SCROLL = 1;
	static final int MENU_SET_MODE = 2;
	static final int MENU_DEMO = 3;

	private LinkedList<String> mListItems;
	private PullToRefreshListView mPullRefreshListView;
	private NewsAdapter mAdapter;
	
	/***
	 * init title
	 */
	void initTabPagerIndicator() {
		tv_indicators = new ArrayList<TextView>();
		for (int i = 0; i < title.length; i++) {
			TextView textView = new TextView(this);
			textView.setText(title[i]);
			textView.setTextSize(20);
			textView.setBackgroundResource(android.R.color.transparent);
			textView.setTextColor(getResources().getColor(R.color.tabpager_indicator_color_normal));
			textView.setGravity(Gravity.CENTER);
			textView.setPadding(20, 10, 20, 10);
			textView.setId(i);
			textView.setOnClickListener(this);
			tv_indicators.add(textView);
			linearLayout.addView(textView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.home);
		linearLayout = (LinearLayout) findViewById(R.id.ll_main);
		initTabPagerIndicator();
		setSelector(0);
		
		mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);

		// Set a listener to be invoked when the list should be refreshed.
		mPullRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {
			@Override
			public void onRefresh(PullToRefreshBase<ListView> refreshView) {
				String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(),
						DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);

				// Update the LastUpdatedLabel
				refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);

				// Do work to refresh the list here.
				new GetDataTask().execute();
			}
		});

		// Add an end-of-list listener
		mPullRefreshListView.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {

			@Override
			public void onLastItemVisible() {
//				Toast.makeText(mContext, "End of List!", Toast.LENGTH_SHORT).show();
			}
		});

		ListView actualListView = mPullRefreshListView.getRefreshableView();

		// Need to use the Actual ListView when registering for Context Menu
		registerForContextMenu(actualListView);

		mListItems = new LinkedList<String>();
		mListItems.addAll(Arrays.asList(mStrings));

		mAdapter = new NewsAdapter(mContext, mListItems);

		
		List<String> values = new ArrayList<String>();
		values.add("1");
		values.add("1");
		values.add("1");
		values.add("1");
		
		PictureGallery gallery = new PictureGallery(mContext);
		gallery.setAdapter(new PictureAdapter(mContext, values));
		actualListView.addHeaderView(gallery);
		
		
		
		// You can also just use setListAdapter(mAdapter) or
		// mPullRefreshListView.setAdapter(mAdapter)
		actualListView.setAdapter(mAdapter);
		
	
	}

	private class GetDataTask extends AsyncTask<Void, Void, String[]> {

		@Override
		protected String[] doInBackground(Void... params) {
			// Simulates a background job.
			try {
				Thread.sleep(4000);
			} catch (InterruptedException e) {
			}
			return mStrings;
		}

		@Override
		protected void onPostExecute(String[] result) {
			mListItems.addFirst("Added after refresh...");
			mAdapter.notifyDataSetChanged();

			// Call onRefreshComplete when the list has been refreshed.
			mPullRefreshListView.onRefreshComplete();

			super.onPostExecute(result);
		}
	}
	
	
	
	@Override
	public void onClick(View v) {
		setSelector(v.getId());
	}

	public void setSelector(int id) {
		for (int i = 0; i < title.length; i++) {
			if (id == i) {
				tv_indicators.get(id).setBackgroundResource(R.drawable.base_tabpager_indicator_selected_pressed);
				tv_indicators.get(id).setTextColor(getResources().getColor(R.color.tabpager_indicator_color_selected));
				tv_indicators.get(id).setPadding(20, 10, 20, 10);
			} else {
				tv_indicators.get(i).setBackgroundResource(android.R.color.transparent);
				tv_indicators.get(i).setTextColor(getResources().getColor(R.color.tabpager_indicator_color_normal));
				tv_indicators.get(i).setPadding(20, 10, 20, 10);
			}
		}
	}
	
	private String[] mStrings = { "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi",
			"Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre",
			"Allgauer Emmentaler", "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi",
			"Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre",
			"Allgauer Emmentaler" };
}